package com.tuju.jetpackfirstdemo.data

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.tuju.jetpackfirstdemo.data.model.City
import com.tuju.jetpackfirstdemo.data.model.County
import com.tuju.jetpackfirstdemo.data.model.Province
import com.tuju.jetpackfirstdemo.network.CoolWeatherNetwork
import com.tuju.jetpackfirstdemo.utils.CoolWeatherExecutors
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class PlaceRepository private constructor(private val network: CoolWeatherNetwork) {

    //获取省列表
    fun getProvinceList(): LiveData<Resource<List<Province>>> {
        val liveData = MutableLiveData<Resource<List<Province>>>()
        liveData.value = Resource.loading(null)
        //判断，如果有网络拿网络数据，并更新本地数据库。无网络拿本地数据库缓存
        network.fetchProvinceList(object : Callback<List<Province>> {
            override fun onFailure(call: Call<List<Province>>, t: Throwable) {
                t.printStackTrace()
                liveData.postValue(Resource.error("加载失败", null))
            }

            override fun onResponse(call: Call<List<Province>>, response: Response<List<Province>>) {
                CoolWeatherExecutors.diskIO.execute {
                    val result = response.body()
                    liveData.postValue(Resource.success(result))
                }
            }
        })
        return liveData
    }

    //市列表
    fun getCityList(provinceId: Int): LiveData<Resource<List<City>>> {
        val liveData = MutableLiveData<Resource<List<City>>>()
        liveData.postValue(Resource.loading(null))

        network.fetchCityList(provinceId, object : Callback<List<City>> {
            override fun onFailure(call: Call<List<City>>, t: Throwable) {
                t.printStackTrace()
                liveData.postValue(Resource.error("加载失败" + t.message, null))
            }

            override fun onResponse(call: Call<List<City>>, response: Response<List<City>>) {
                CoolWeatherExecutors.diskIO.execute {
                    val result = response.body()
                    liveData.postValue(Resource.success(result))
                }
            }
        })
        return liveData
    }

    //区列表
    fun getCountyList(provinceId: Int, cityId: Int): LiveData<Resource<List<County>>> {
        val liveData = MutableLiveData<Resource<List<County>>>()
        liveData.postValue(Resource.loading(null))

        network.fetchCountyList(provinceId, cityId, object : Callback<List<County>> {
            override fun onFailure(call: Call<List<County>>, t: Throwable) {
                t.printStackTrace()
                liveData.postValue(Resource.error("加载失败" + t.message, null))
            }

            override fun onResponse(call: Call<List<County>>, response: Response<List<County>>) {
                CoolWeatherExecutors.diskIO.execute {
                    val result = response.body()
                    liveData.postValue(Resource.success(result))
                }
            }
        })

        return liveData
    }

    companion object {
        private var instance: PlaceRepository? = null

        fun getInstantce(network: CoolWeatherNetwork): PlaceRepository {
            if (instance == null) {
                synchronized(PlaceRepository::class.java) {
                    if (instance == null) {
                        instance = PlaceRepository(network)
                    }
                }
            }
            return instance!!
        }
    }

}